<script setup lang="ts">
import logo from "@/assets/logo.png";
import logo2 from "@/assets/logo2.png";
import { onMounted, ref, onBeforeUnmount } from "vue";
import { GetLineName } from "./api/index";
import dayjs from "dayjs";

import type { TabsPaneContext } from "element-plus";
import DeviceControl from "./views/components/DeviceControl.vue";
import DeviceMonitor from "./views/components/DeviceMonitor.vue";
import DataSearch from "./views/components/DataSearch.vue";

const activeName = ref("first");
const refreshChart = ref(false);
const handleClick = (tab: TabsPaneContext, event: Event) => {
  if (tab.index === "1") {
    refreshChart.value = true;
  }
};

const runStatus = ref("pending");
const updateStatus = (status: string) => {
  runStatus.value = status;
};
const deviceList = ref([]);
const updateDevice = (list: any) => {
  deviceList.value = list;
};
const logList = ref([]);
const updateLog = (list: any) => {
  logList.value = list;
};
const sstInfo = ref({});
const updateSST = (list: any) => {
  sstInfo.value = list;
};
//获取故障信息
const errorList = ref([]);
const updateFault = (list: any) => {
  errorList.value = list;
};
//获取急停信息
const alarmList = ref([]);
const updateAlarm = (list: any) => {
  alarmList.value = list;
};

const lineName = ref("流水线控制系统");
const lineValue = ref();
const getNameInfo = () => {
  GetLineName().then((res: any) => {
    if (res.code === 200) {
      lineName.value = res.data.length && res.data[0].Label;
      lineValue.value = res.data.length && res.data[0].Value;
    }
  });
};
const updateLineName = () => {
  getNameInfo();
};

const current = dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss");
let currentTime = ref(current);
let timerID = ref();
onMounted(() => {
  timerID.value = setInterval(() => {
    currentTime.value = dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss");
  }, 1000);
  getNameInfo();
});
// import bus from 'vue3-eventbus'

// const userID = ref();
// function getUser() {
//   const user = localStorage.getItem("USER_ID");
//   userID.value = user;
// }
// bus.on("getUser", getUser);
// //   在组件卸载之前移除侦听
onBeforeUnmount(() => {
  clearInterval(timerID.value);
  timerID.value = null;
});
</script>

<template>
  <div class="overflow-hidden main">
    <div class="header">
      <img class="left-logo" :src="logo" width="170" height="45" />
      <div class="header-title">
        <p class="text-4xl">{{ lineName || "流水线控制系统" }}</p>
      </div>
      <img class="right-logo" :src="logo2" width="100" height="46" />
    </div>
    <section class="flex">
      <div class="content">
        <el-tabs class="my-tabs" v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="设备控制" name="first">
            <DeviceControl
              @updateStatus="updateStatus"
              @updateDevice="updateDevice"
              @updateLog="updateLog"
              @updateFault="updateFault"
              @updateAlarm="updateAlarm"
              @updateSST="updateSST"
            />
          </el-tab-pane>
          <el-tab-pane label="设备监控" name="second">
            <DeviceMonitor
              :deviceList="deviceList"
              :logList="logList"
              :errorList="errorList"
              :alarmList="alarmList"
              :sstInfo="sstInfo"
            />
          </el-tab-pane>
          <!-- <el-tab-pane label="设备参数" name="third">设备参数</el-tab-pane> -->
          <el-tab-pane label="数据查询" name="third">
            <DataSearch
              :lineValue="lineValue"
              @updateLineName="updateLineName"
            />
          </el-tab-pane>
        </el-tabs>
        <div class="current-time">{{ currentTime }}</div>
        <div class="run-status">
          <span
            v-if="runStatus === 'running'"
            class="text-3xl text-green-500 font-semibold"
            type="success"
            >运行中</span
          >
          <span
            v-if="runStatus === 'pending'"
            class="text-3xl text-amber-500 font-semibold"
            type="warning"
            >待启动</span
          >
        </div>
      </div>
    </section>
  </div>
</template>
<style lang="less">
.main {
  background: #0d0f20;
  color: #fff;
  font-size: 1.5rem;
}

.header {
  position: relative;
  height: 80px;
  color: #fff;
  line-height: 80px;
  text-align: center;

  .left-logo {
    position: absolute;
    object-fit: contain;
    top: 5px;
    left: 10px;
    height: 45px;
    align-items: center;
  }

  .right-logo {
    position: absolute;
    top: 2px;
    right: 10px;
    object-fit: contain;
  }

  .header-title {
    background: url(./assets/header-bg.png) no-repeat center center;
    p {
      font-weight: 400;
      font-size: 30px;
      color: #ffffff;
      line-height: 62px;
    }
  }
}

.content {
  position: relative;
  width: 100%;
  height: calc(100vh - 80px);
  overflow: hidden;
  padding: 0 10px;
}

.el-tabs__item {
  width: 147px;
  height: 32px;
  background: url(./assets/tab-bg.png) no-repeat center center;
  color: #fff!important;
  font-size: 18px!important;
  padding: 0!important;
}
.el-tabs__item.is-active {
  background: url(./assets/tab-bg-actived.png) no-repeat center center;
  color: var(--el-color-primary);
}

.el-tabs__nav-wrap::after {
  display: none;
}
.el-tabs__active-bar {
  display: none;
}
.run-status {
  position: absolute;
  right: 20px;
  top: -5px;
}
.current-time {
  position: absolute;
  right: 120px;
  top: -3px;
}
</style>
